package com.molichuxing.services.infrastructure.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.dto.request.create.OrderDealerCreateDto;
import com.molichuxing.services.infrastructure.dto.response.OrderDealerDto;
import com.molichuxing.services.property.OrderDealerStatusEnum;
import com.molichuxing.services.property.OrderRequestTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.services.infrastructure.bean.OrderDealerBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.OrderDealerMapper;
import com.molichuxing.services.infrastructure.dto.response.OrderDealerDetailBasicDto;
import com.molichuxing.services.infrastructure.service.OrderDealerService;

/**
 * <p>
 * 经销商订单表 服务实现类
 * </p>
 *
 * @author renzhengyu
 * @since 2019-08-12
 */
@Service("orderDealerService")
public class OrderDealerServiceImpl  implements OrderDealerService {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderDealerServiceImpl.class);


    @Autowired
    private OrderDealerMapper orderDealerMapper;

    @Override
    public Paged<OrderDealerDto> getOrderDealerList(Map<String, Object> params, Integer pageNum, Integer pageSize, OrderRequestTypeEnum requestType) {


        if(null != params.get("startTime")){
            params.put("startTime",ObjectCastUtil.getDayStart(params.get("startTime")));
        }
        if(null != params.get("endTime")){
            params.put("endTime",ObjectCastUtil.getDayEnd(params.get("endTime")));
        }

        List<TabDto> tabList = new ArrayList<>();
        TabDto tabDto = new TabDto();
        tabDto.setName("全部");
        tabDto.setCount(orderDealerMapper.getOrderDealerAllCount(params));
        tabList.add(tabDto);
        int statusCount;
        for(OrderDealerStatusEnum statusEnum : OrderDealerStatusEnum.values()) {
            //创建订单不做统计
            if(OrderDealerStatusEnum.CREATE_ORDER.getValue() == statusEnum.getValue()){
                continue;
            }
            if(OrderRequestTypeEnum.DEALER.getValue() == requestType.getValue()){
                Integer dealerId = (Integer) params.get("dealerId");
                statusCount = orderDealerMapper.getOrderDealerStatusCount(statusEnum.getValue(),dealerId);
            }else{
                statusCount = orderDealerMapper.getOrderStatusCount(statusEnum.getValue());
            }
            tabDto = new TabDto();
            tabDto.setCount(statusCount);
            tabDto.setName(statusEnum.getName());
            tabDto.setType(statusEnum.getValue());
            tabList.add(tabDto);
        }
        int count = orderDealerMapper.getOrderDealerCount(params);
        if(count <= 0) {
            return new Paged<>(null,tabList,0,pageSize);
        }
        List<OrderDealerBean> list = orderDealerMapper.getOrderDealerList(params, pageNum, pageSize);
        Paged<OrderDealerDto> orderDealerDtoPage = new Paged<>(Convert.toOrderDealerDto(list), count, pageSize);
        orderDealerDtoPage.setTab(tabList);
        return orderDealerDtoPage;
    }

    @Override
    public OrderDealerDetailBasicDto getDetailBasic(Long orderCode) {
        OrderDealerBean orderDealerBeanInfo = orderDealerMapper.getDetailBasic(orderCode);
        return Convert.toOrderDealerDetailBasicDTO(orderDealerBeanInfo);
    }

    @Override
    public Integer getOrderDealerStatus(Long orderCode) {

        return orderDealerMapper.getOrderDealerStatusByOrderCode(orderCode);
    }

    @Override
    public Integer createOrderDealer(OrderDealerCreateDto orderDealerCreateDto) {

        OrderDealerBean bean = new OrderDealerBean();
        BeanUtils.copyProperties(orderDealerCreateDto, bean);

        return orderDealerMapper.create(bean);
    }

    @Override
    public Boolean getContractNo(String contractNo) {

        OrderDealerBean bean = orderDealerMapper.getContractNo(contractNo);
        if(bean == null){
            return false;
        }
        return true;
    }

    @Override
    public Boolean modifyOrderStatus(Long orderCode, Integer orderStatus) {
        return (orderDealerMapper.updateOrderStatus(orderCode, orderStatus) == 1) ? true : false;
    }

    @Override
    public Integer getCarCount(Long orderCode) {
        return orderDealerMapper.getCarCount(orderCode);
    }

    @Override
    public List<Long> getOrderCodeByDealerId(Integer dealerId) {
        return orderDealerMapper.getOrderCodeByDealerId(dealerId);
    }

}
